import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    strict: true,
    state: {
        products: [
            { name: "马云", price: 200 },
            { name: "马化腾", price: 100 },
            { name: "马冬梅", price: 10 },
            { name: "马蓉", price: 1 }
        ]
    },
    getters: {
        // state是上面的state所以不需要this.$store
        saleProducts: state => {
            let saleProducts = state.products.map(product => {
                return {
                    name: "**" + product.name + "**",
                    price: product.price / 2
                };
            });
            return saleProducts;
        }

    },
    mutations: {
        reducePrice: (state, playload) => {
            state.products.forEach(product => {
                product.price -= playload
            })
        }
    },
    actions: {
        reducePrices: (context, playload) => {
            setTimeout(() => {
                context.commit('reducePrice', playload)
            }, 2000)
        }
    },
    modules: {}
})